package chatclient.applets.note;


import chatclient.applets.ChatView;
import chatclient.applets.canvas.ImageCanvas;
import chatclient.tools.NoteContainer;
import chatclient.tools.NoteListener;
import chatclient.applets.components.Border;
import chatclient.netio.Message;
import chatclient.netio.Event;
import chatclient.netio.ClientAttr;
import chatclient.netio.AttrList;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.Enumeration;
import java.util.Vector;


public class Note
        implements ImageObserver {

    private final Object sync;
    private NoteListener listener;
    private NoteContainer container;
    private Border border;
    private String nickName;
    private String channelName;
    private Vector pfxParts;
    private Vector bodyParts;
    private Color bgcolor;
    private boolean fixWidth;
    private int maxWidth;
    private int width;
    private int height;
    private LinePart pfxLine;
    private Vector bodyLines;

    private Note(NoteListener notelistener, NoteContainer notecontainer, int i) {
        sync = new Object();
        listener = notelistener;
        container = notecontainer;
        nickName = null;
        channelName = null;
        pfxParts = new Vector();
        bodyParts = new Vector();
        maxWidth = i;
        width = -1;
        height = -1;
        bodyLines = new Vector();
    }

    public static Note fromTemplate(NoteListener notelistener, NoteContainer notecontainer, int i, String s, String s1, AttrList attrlist, Font font, FontMetrics fontmetrics) {
        System.out.println("ADZ - Note.java->fromTemplate:s="+s+":s1="+s1+":attrlist="+attrlist);
        
        Note note = null;
        int flag = 0;
        try {
            if (s != null) {
                note = new Note(notelistener, notecontainer, i);
                note.nickName = s1;
                note.expand(s, attrlist, note.pfxParts, note.bodyParts, font, fontmetrics, flag);
                note.layout();
            }
        } catch (Exception exception) {
            note = null;
            exception.printStackTrace();
        }
        return note;
    }

    public static Note fromTemplate(NoteListener notelistener, NoteContainer notecontainer, int i, String s, String s1, AttrList attrlist, Font font, FontMetrics fontmetrics, int flag) {
        
        System.out.println("ADZ - Note.java->fromTemplate:s="+s+":s1="+s1+":attrlist="+attrlist);
        Note note = null;
        try {
            if (s != null) {
                note = new Note(notelistener, notecontainer, i);
                note.nickName = s1;
                note.expand(s, attrlist, note.pfxParts, note.bodyParts, font, fontmetrics, flag);
                note.layout();
            }
        } catch (Exception exception) {
            note = null;
            exception.printStackTrace();
        }
        return note;
    }    
    
    public static Note fromEvent(NoteListener notelistener, NoteContainer notecontainer, int i, Event event, String s, Font font, FontMetrics fontmetrics) {
        
        System.out.println("ADZ - Note.java->fromEvent:Event="+event+":s="+s);
        String s1 = null;
        String s2 = null;
        AttrList attrlist = event.getParams();
        boolean flag = false;
        boolean flag1 = false;
        String s3 = null;
        Note note = null;
        if (event == null) {
            return null;
        }
        try {
            Enumeration enumeration = attrlist.getEnumeration();
            int j = 0;
            int k = 0;
            while (enumeration.hasMoreElements()) {
                ClientAttr clientattr = (ClientAttr) enumeration.nextElement();
                switch (clientattr.getId()) {
                    case ClientAttr.NICKNAME:
                        s1 = clientattr.getString();
                        j++;
                        break;

                    case ClientAttr.CHANNELNAME:
                        s2 = clientattr.getString();
                        k++;
                        break;
                }
            }
            if (j > 1) {
                s1 = null;
            }
            if (k > 1) {
                s2 = null;
            }
            if (s1 != null) {
                flag = s1.equalsIgnoreCase(notecontainer.getChat().getMyName());
            }
            flag1 = attrlist.get(ClientAttr.TARGETNAME) != null && attrlist.get(ClientAttr.TARGETNAME).getString().equalsIgnoreCase(notecontainer.getChat().getMyName());
        } catch (Exception exception) {
            s1 = null;
        }
        if (flag) {
            if (s != null) {
                s3 = notecontainer.getParameter((new StringBuilder("my")).append(s).append(event.getName()).toString().toLowerCase());
            }
            if (s3 == null) {
                s3 = notecontainer.getParameter((new StringBuilder("my")).append(event.getName()).toString().toLowerCase());
            }
        }
        if (s3 == null && flag1) {
            if (s != null) {
                s3 = notecontainer.getParameter((new StringBuilder("me")).append(s).append(event.getName()).toString().toLowerCase());
            }
            if (s3 == null) {
                s3 = notecontainer.getParameter((new StringBuilder("me")).append(event.getName()).toString().toLowerCase());
            }
        }
        if (s3 == null) {
            if (s != null) {
                s3 = notecontainer.getParameter((new StringBuilder(String.valueOf(s))).append(event.getName().toLowerCase()).toString());
            }
            if (s3 == null) {
                s3 = notecontainer.getParameter(event.getName().toLowerCase());
            }
        }
        if (s3 != null) {
            try {
                note = fromTemplate(notelistener, notecontainer, i, s3, s1, event.getParams(), font, fontmetrics);
                if (note != null) {
                    note.channelName = s2;
                }
            } catch (Exception exception1) {
                exception1.printStackTrace();
                System.err.println(event.toString());
            }
        }
        return note;
    }

    public String getNick() {
        return nickName;
    }

    public String getChannel() {
        return channelName;
    }

    public Double getMinColorDistance() {
        return container.getMinColorDistance();
    }

    public Color getBackground(String s) {
        Color color = container.getNickBackground(getNick(), s);
        if (color == null) {
            color = container.getChannelBackground(getChannel(), s);
        }
        if (color == null) {
            color = container.getBackground(s);
        }
        return color != null ? color : bgcolor;
    }
    
    public Color getBackgroundForBgImage(String s) {
        Color color = container.getNickBackground(getNick(), s);
        
        return color;
    }
    
    public Color getForeground(String s) {
        Color color = container.getNickColor(getNick(), s);
        if (color == null) {
            color = container.getChannelColor(getChannel(), s);
        }
        if (color == null) {
            color = container.getForeground(s);
        }
        return color;
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int i) {
        setWidth(i, true);
    }

    private void setWidth(int i, boolean flag) {
        if (maxWidth != i || fixWidth != flag) {
            maxWidth = i;
            fixWidth = flag;
            layout();
        }
    }

    public int getMaxWidth() {
        return maxWidth;
    }

    public void setMaxWidth(int i) {
        setWidth(i, false);
    }

    public void setBorder(Border border1) {
        border = border1;
        layout();
    }

    private void expand(String s, AttrList attrlist, Vector vector, Vector vector1, Font font, FontMetrics fontmetrics, int flag)
            throws Exception {
        
        System.out.println("ADZ - Note.java->expand:s="+s+":vectore="+vector+":vector1="+ vector1 +":attrlist="+attrlist);
        StringBuffer stringbuffer = new StringBuffer();
        StringBuffer stringbuffer1 = new StringBuffer();
        StringBuffer stringbuffer2 = new StringBuffer();
        Message.expand(s, attrlist, new StringBuffer[]{
                    stringbuffer, stringbuffer1, stringbuffer2
                }, flag, container.getFilters());
        breakIntoParts(new Vector[]{
                    vector, vector1
                }, new String[]{
                    stringbuffer.toString(), stringbuffer1.toString()
                }, font, fontmetrics);
        if (stringbuffer2.length() > 0) {
            bgcolor = Color.decode(stringbuffer2.toString());
        }
        
        System.out.println("ADZ - Note.java->expand:After breakIntoPart:vector="+vector+":vector1="+ vector1);
    }

    private void breakIntoParts(Vector avector[], String as[], Font font, FontMetrics fontmetrics)
            throws Exception {
        
        System.out.println("ADZ - Note.java->breakIntoParts:as[0]="+as[0]+":as[1]="+as[1]);
        
        Color color = null;
        Vector vector = new Vector();
        Vector vector1 = new Vector();
        Vector vector2 = new Vector();
        Vector vector3 = new Vector();
        for (int i = 0; i < as.length; i++) {
            String s = as[i];
            int j = 0;
            for (int k = 0; k < s.length(); k++) {
                switch (s.charAt(k)) {
                    case 27:
                        if (j < k - 1) {
                            avector[i].addElement(new TextPart(s.substring(j, k), false, font, fontmetrics, color));       
                        }
                        if (++k < s.length()) {
                            char c = s.charAt(k++);
                            j = k;
                            for (; k < s.length() && s.charAt(k) != ';'; k++) {
                            }
                            char c1;
                            label0:
                            switch (c) {
                                case 68:
                                case 69:
                                case 72:
                                case 74:
                                case 75:
                                case 76:
                                case 77:
                                case 78:
                                case 79:
                                case 81:
                                case 82:
                                case 84:
                                case 85:
                                case 86:
                                case 87:
                                case 88:
                                case 89:
                                case 90:
                                case 91:
                                case 92:
                                case 93:
                                case 94:
                                case 95:
                                case 96:
                                case 97:
                                case 100:
                                case 101:
                                case 104:
                                case 106:
                                case 107:
                                case 108:
                                case 109:
                                case 110:
                                case 111:
                                case 113:
                                case 114:
                                default:
                                    break;

                                case 66:
                                case 67:
                                case 70:
                                case 71:
                                case 73:
                                case 80:
                                case 83:
                                    vector1.addElement(font);
                                    vector2.addElement(fontmetrics);
                                    vector3.addElement(color);
                                    switch (c) {
                                        case 70:
                                            vector.addElement(new Character('f'));
                                            font = new Font(s.substring(j, k), font.getStyle(), font.getSize());
                                            fontmetrics = container.getFontMetrics(font);
                                            break;

                                        case 66:
                                            vector.addElement(new Character('b'));
                                            font = new Font(font.getName(), (font.getStyle() | 1) & -1, font.getSize());
                                            fontmetrics = container.getFontMetrics(font);
                                            break;

                                        case 73:
                                            vector.addElement(new Character('i'));
                                            font = new Font(font.getName(), (font.getStyle() | 2) & -1, font.getSize());
                                            fontmetrics = container.getFontMetrics(font);
                                            break;

                                        case 80:
                                            vector.addElement(new Character('p'));
                                            font = new Font(font.getName(), 0, font.getSize());
                                            fontmetrics = container.getFontMetrics(font);
                                            break;

                                        case 83:
                                            vector.addElement(new Character('s'));
                                            switch (s.charAt(j)) {
                                                case 43:
                                                    j++;
                                                    break;

                                                case 44:
                                                default:
                                                    j = k;
                                                    break;

                                                case 45:
                                                    break;
                                            }
                                            if (j < k) {
                                                try {
                                                    font = new Font(font.getName(), font.getStyle(), font.getSize() + Integer.parseInt(s.substring(j, k)));
                                                    fontmetrics = container.getFontMetrics(font);
                                                } catch (Exception exception) {
                                                    exception.printStackTrace();
                                                }
                                            }
                                            break;

                                        case 67:
                                            vector.addElement(new Character('c'));
                                            try {
                                                color = Color.decode(s.substring(j, k));
                                            } catch (Exception exception1) {
                                                exception1.printStackTrace();
                                            }
                                            break;

                                        case 71:
                                             System.out.println("ADZ - Note.java->breakIntoParts:In 71. c="+c);
                                            vector.addElement(new Character('g'));
                                            BufferedImage img = container.getSmileyImage(s.substring(j, k));
                                            if(img != null)
                                            {
                                              avector[i].addElement(new ImagePart(img, this));
                                            }
                                            break;
                                    }
                                    break;

                                case 98:
                                case 99:
                                case 102:
                                case 103:
                                case 105:
                                case 112:
                                case 115:
                                    do {
                                        if (vector.size() <= 0) {
                                            break label0;
                                        }
                                        c1 = ((Character) vector.elementAt(vector.size() - 1)).charValue();
                                        vector.removeElementAt(vector.size() - 1);
                                        font = (Font) vector1.elementAt(vector1.size() - 1);
                                        vector1.removeElementAt(vector1.size() - 1);
                                        fontmetrics = (FontMetrics) vector2.elementAt(vector2.size() - 1);
                                        vector2.removeElementAt(vector2.size() - 1);
                                        color = (Color) vector3.elementAt(vector3.size() - 1);
                                        vector3.removeElementAt(vector3.size() - 1);
                                    } while (c != c1);
                                    break;
                            }
                            j = k + 1;
                        }
                        break;

                    case 10:
                        avector[i].addElement(new TextPart(s.substring(j, k), true, font, fontmetrics, color));
                        j = k + 1;
                        break;
                }
            }

            if (j < s.length()) {
                avector[i].addElement(new TextPart(s.substring(j), false, font, fontmetrics, color));
            }
        }

    }

    public void layout() {
        boolean flag = false;
        synchronized (sync) {
            int i = 0;
            int j = 0;
            pfxLine = new LinePart(0);
            for (Enumeration enumeration = pfxParts.elements(); enumeration.hasMoreElements(); pfxLine.add((NotePart) enumeration.nextElement(), true)) {
            }
            bodyLines.removeAllElements();
            int k = 0;
            int l = getMaxWidth() - pfxLine.getWidth();
            LinePart linepart = null;
            if (border != null) {
                i += border.getTotalWidth();
                j += border.getTotalHeight();
                l -= border.getTotalWidth();
            }
            for (Enumeration enumeration1 = bodyParts.elements(); enumeration1.hasMoreElements();) {
                for (NotePart notepart = (NotePart) enumeration1.nextElement(); notepart != null;) {
                    if (linepart == null) {
                        linepart = new LinePart(l);
                    }
                    if (linepart.add(notepart, false)) {
                        notepart = null;
                        if (linepart.hasEol()) {
                            bodyLines.addElement(linepart);
                            k += linepart.getHeight();
                            linepart = null;
                        }
                    } else {
                        NotePart notepart1 = null;
                        if ((notepart1 = notepart.breakAtWidth(l - linepart.getWidth(), false, linepart)) != notepart) {
                            notepart = notepart1;
                        } else if (linepart.getWidth() == 0) {
                            notepart = notepart.breakAtWidth(l - linepart.getWidth(), true, linepart);
                        }
                        bodyLines.addElement(linepart);
                        k += linepart.getHeight();
                        linepart = null;
                    }
                }

            }

            if (linepart != null) {
                bodyLines.addElement(linepart);
                k += linepart.getHeight();
            }
            j += Math.max(pfxLine.getHeight(), k);
            Enumeration enumeration2 = bodyLines.elements();
            int i1 = 0;
            while (enumeration2.hasMoreElements()) {
                int j1 = ((LinePart) enumeration2.nextElement()).getWidth();
                if (j1 > i1) {
                    i1 = j1;
                }
            }
            i += pfxLine.getWidth() + i1;
            if (fixWidth) {
                i = maxWidth;
            }
            flag = j != height;
            width = i;
            height = j;
        }
        if (listener != null && flag) {
            listener.noteHeightChanged(this);
        }
    }

    public void paint(Graphics g, int i, int j, boolean forceBgColor) {

        paint(g, i, j, null, forceBgColor);
    }

    public void paint(Graphics g, int i, int j, String s, boolean forceBgColor) {
        synchronized (sync) {
            Color color;
            if(forceBgColor)
            {
                color = getBackground(s);
            }
            else
            {
                color = getBackgroundForBgImage(s);
            }
            Double double1 = getMinColorDistance();
            int k = border != null ? i + border.getLeftWidth() : i;
            int l = border != null ? j + border.getTopHeight() : j;
            if (color != null) {
                Color color1 = g.getColor();
                g.setColor(color);
                g.fillRect(k, l, getWidth(), getHeight());
                g.setColor(color1);
            }
            NotePart notepart = bodyLines.size() > 0 ? (NotePart) bodyLines.elementAt(0) : null;
            if (pfxLine != null) {
                pfxLine.paint(container, this, g, k, l + (notepart != null ? Math.max(0, (notepart.getLeading() + notepart.getAscent()) - (pfxLine.getLeading() + pfxLine.getAscent())) : 0), s, color, double1);
                k += pfxLine.getWidth();
            }

            for (Enumeration enumeration = bodyLines.elements(); enumeration.hasMoreElements();) {
                NotePart notepart1 = (NotePart) enumeration.nextElement();
                notepart1.paint(container, this, g, k, l, s, color, double1);
                l += notepart1.getHeight();
            }

        }
        if (border != null) {
            border.paint(this, g, i, j, getWidth() - border.getTotalWidth(), getHeight() - border.getTotalHeight());
        }
    }

    @Override
    public boolean imageUpdate(Image image, int i, int j, int k, int l, int i1) {

        if ((i & WIDTH) != 0 || (i & HEIGHT) != 0) {
            layout();
        }

        boolean ready = (i & (ERROR | ABORT | ALLBITS)) != 0;
        boolean render = (i & (FRAMEBITS | ALLBITS)) != 0;
        
        if (render) {
           listener.noteChanged(this);
        }
        

        return !ready;
    }    
}
